package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.wgt.test.bean.Product;
import com.stu.wgt.test.bean.ProductPictrurl;
import com.stu.wgt.test.mapper.ProductCategoriesMapper;
import com.stu.wgt.test.mapper.ProductMapper;
import com.stu.wgt.test.mapper.ProductPictrurlMapper;
import com.stu.wgt.test.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-01-20
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductPictrurlMapper productPictrurlMapper;

    @Override
    public int insert(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public int delete(int id) {
        return productMapper.deleteById(id);
    }

    @Override
    public int update(Product product) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("id", product.getId());
        return productMapper.update(product, qw);
    }

    @Override
    public List<Product> selectAllProduct() {
        List<Product> productList = productMapper.selectList(null);
        associateProductImages(productList);
        return productList;
    }

    @Override
    public List<Product> selectById(int id) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("id", id);
        List<Product> productList = productMapper.selectList(qw);
        associateProductImages(productList);
        return productList;
    }

    @Override
    public List<Product> selectByIds(List<Integer> ids) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.in("id", ids);
        return productMapper.selectList(qw);
    }

    @Override
    public List<Product> selectByName(String name) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("name", name);
        List<Product> productList = productMapper.selectList(qw);
        associateProductImages(productList);
        return productList;
    }

    @Override
    public IPage<Product> searchProduct(String query, Integer merchantId, int pageNum, int pageSize) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Product> qw = new QueryWrapper<>();

        if(!query.isEmpty() && query != null) {
            qw.like("name", query);
        }
        // 如果商品id不为空，则进行查询
        if (merchantId != null) {
            qw.eq("merchant_id", merchantId);
        }
        IPage<Product> productIPage = productMapper.selectPage(page, qw);
        List<Product> productList = productIPage.getRecords();
        associateProductImages(productList);
        return productIPage;
    }

    @Override
    public List<Product> selectByCate(Integer id) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("cate_id", id);
        List<Product> productList = productMapper.selectList(qw);
        associateProductImages(productList);
        return productList;
    }

    @Override
    public List<Product> selectCategoriesWithParentId(Integer[] query, int pageNum, int pageSize) {
        if (query == null || query.length == 0) {
            return Collections.emptyList();
        }

        // 将数组转换为列表, MyBatis 通常需要集合或列表作为参数进行查询
        List<Integer> categoryIdList = Arrays.asList(query);

        Page<Product> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.in("cate_id", categoryIdList);

        IPage<Product> productIPage = productMapper.selectPage(page, qw);

        List<Product> productList = productIPage.getRecords();

        associateProductImages(productList);

        return productIPage.getRecords();
    }

    /**
     * 添加商品的图片
     *
     * @param productList
     */
    private void associateProductImages(List<Product> productList) {
        for (Product product : productList) {
            List<ProductPictrurl> picList = productPictrurlMapper.selectList(
                    new QueryWrapper<ProductPictrurl>().eq("product_id", product.getId())
            );

            if (!picList.isEmpty()) {
                product.setUrl(picList.get(0).getPictureUrl());
            } else {
                product.setUrl(null);
            }
        }
    }
}
